﻿using CrazyTalk.Common;

namespace CrazyTalk.Web.UI
{
    using System.Collections.Specialized;
    using System.Web;

    public class HttpHandlerBase : IHttpHandler
    {
        public HttpHandlerBase()
        {
            CurContext = HttpContext.Current;
        }
        public HttpContext CurContext { get; set; }
        public bool IsReusable
        {
            get { return false; }
        }

        public virtual void ProcessRequest(HttpContext context)
        {

        }
        public void AlertAndGo(string msg, string url)
        {
            CurContext.Response.Write(string.Format("<script>alert('{0}');location.href='{1}'</script>", msg, url));
            CurContext.Response.End();
        }

        public NameValueCollection Form()
        {
            return CurContext.Request.Form;
        }

        public string Form(int index)
        {
            return Form()[index];
        }

        public string Form(string key)
        {
            return Form()[key];
        }

        public decimal FormToDecimal(int index)
        {
            return ConvertUtil.ToDecimal(Form()[index]);
        }

        public decimal FormToDecimal(string key)
        {
            return ConvertUtil.ToDecimal(Form()[key]);
        }

        public int FormToInt(int index)
        {
            return ConvertUtil.ToInt(Form()[index]);
        }

        public int FormToInt(string key)
        {
            return ConvertUtil.ToInt(Form()[key]);
        }

        public NameValueCollection Query()
        {
            return CurContext.Request.QueryString;
        }

        public string Query(int index)
        {
            return Query()[index];
        }

        public string Query(string key)
        {
            return Query()[key];
        }

        public decimal QueryToDecimal(int index)
        {
            return ConvertUtil.ToDecimal(Query()[index]);
        }

        public decimal QueryToDecimal(string key)
        {
            return ConvertUtil.ToDecimal(Query()[key]);
        }

        public int QueryToInt(int index)
        {
            return ConvertUtil.ToInt(Query()[index]);
        }

        public int QueryToInt(string key)
        {
            return ConvertUtil.ToInt(Query()[key]);
        }
        /// <summary>
        /// 返回当前请求响应返回对象
        /// </summary>
        protected HttpResponse Response
        {
            get { return CurContext.Response; }
        }
    }
}

